package com.fqgame.user.mq;

import com.fqgame.user.service.UserInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * 积分消息监听器
 */
@Slf4j
@Component
public class PointsMessageListener {

    @Autowired
    private UserInfoService userInfoService;

    /**
     * 处理积分更新消息
     */
    @RabbitListener(queues = "user.points.queue")
    public void handlePointsUpdateMessage(Map<String, Object> message) {
        log.info("收到积分更新消息：{}", message);
        
        try {
            Long userId = Long.valueOf(message.get("userId").toString());
            Integer points = Integer.valueOf(message.get("points").toString());
            String reason = (String) message.get("reason");
            
            userInfoService.updateUserPoints(userId, points, reason);
            log.info("积分更新成功，用户ID：{}，积分：{}", userId, points);
        } catch (Exception e) {
            log.error("处理积分更新消息失败", e);
            // 实际应用中可能需要重试机制或死信队列
            throw e;
        }
    }
}